package com.ershi.spring.aop.proxy.chain;

import com.ershi.spring.aop.proxy.common.TargetMethod;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author 二十
 * @since 2021/10/31 10:05 下午
 */
public class JdkDynamic implements InvocationHandler {

	Object target ;

	AbstractSlot head;

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		return head.proceed(new TargetMethod(
				target,method,args
		));
	}

	public Object getProxy(){
		return Proxy.newProxyInstance(
				target.getClass().getClassLoader(),
				target.getClass().getInterfaces(),
				this
		);
	}

	public JdkDynamic(Object target, AbstractSlot head) {
		this.target = target;
		this.head = head;
	}
}
